﻿using MarriageFertility.Helpers;
using TaleWorlds.CampaignSystem;
using TaleWorlds.CampaignSystem.MapEvents;

namespace MarriageFertility.Behaviors
{
    public class TavernSpouseBehavior : CampaignBehaviorBase
    {
        private CampaignGameStarter _currentStarter;

        public override void RegisterEvents()
        {
            CampaignEvents.OnSessionLaunchedEvent.AddNonSerializedListener(this, OnSessionLaunched);
        }

        public override void SyncData(IDataStore dataStore)
        {
            
        }

        private void OnSessionLaunched(CampaignGameStarter starter)
        {
            _currentStarter = starter;
            RegisterTavernMaidConversations(starter);
        }

        private void RegisterTavernMaidConversations(CampaignGameStarter starter)
        {
            string[] inputTokens = [
                "cprostitute_talk_00_response",
                "cprostitute_talk_01_response",
                "cprostitute_talk_02_response",
                "tcustomer_00",
                "ccustomer_00",
                "town_or_village_player",
                "tavernmaid_talk"
            ];

            foreach (var inputToken in inputTokens)
            {
                starter.AddPlayerLine(
                    "sms_tavernmaid_chat_player",
                    inputToken,
                    "mf_tavernmaid_start",
                    "{=MF_TavernMaidChat}I have something to discuss with you",
                    IsNormalNPC,
                    null,
                    100);
            }

            starter.AddDialogLine(
                "sms_tavernmaid_ask_what",
                "mf_tavernmaid_start",
                "sms_tavernmaid_ask_what",
                "{=MF_TavernMaidAskWhat}Oh? Tell me about it",
                null,
                null,
                100);

            starter.AddPlayerLine(
                "sms_tavernmaid_ask_what_result_spouse",
                "sms_tavernmaid_ask_what",
                "sms_tavernmaid_accept_result",
                "{=MF_TavernMaidProposeSpouse}Give up everything and become my spouse",
                IsNormalNPC,
                () => RelationHelper.NameLessNPCToSpouse(CharacterObject.OneToOneConversationCharacter, _currentStarter),
                100);

            starter.AddPlayerLine(
                "sms_tavernmaid_ask_what_result_companion",
                "sms_tavernmaid_ask_what",
                "sms_tavernmaid_accept_result",
                "{=MF_TavernMaidProposeCompanion}Give up everything and join my clan",
                IsNormalNPC,
                () => RelationHelper.NameLessNPCToLord(CharacterObject.OneToOneConversationCharacter, _currentStarter));

            starter.AddPlayerLine(
                "sms_tavernmaid_ask_what_result_cancel",
                "sms_tavernmaid_ask_what",
                "close_window",
                "{=MF_TavernMaidCancel}Just kidding",
                null,
                null);

            starter.AddDialogLine(
                "sms_tavernmaid_accept_result",
                "sms_tavernmaid_accept_result",
                "mf_companion_become_spouse_accept",
                "{=MF_TavernMaidAccept}Thank you, you've opened a wonderful new life for me",
                null,
                null);
        }

        private bool IsPlayerSpouse() =>
            Hero.OneToOneConversationHero.IsPlayerSpouse();
        

        private bool IsPlayerCompanion() =>
            Hero.OneToOneConversationHero?.IsPlayerCompanion ?? false;

        private bool IsNormalNPC() =>
            CharacterObject.OneToOneConversationCharacter != null &&
            !IsPlayerCompanionAndCanBecomeSpouse() &&
            !IsPlayerSpouse();

        private bool IsPlayerCompanionAndCanBecomeSpouse() => IsPlayerCompanion();
    }
}